package com.ljf.tijian.Interceptor;


import com.ljf.tijian.ApiResponse;
import com.ljf.tijian.exception.TijianExceptionEnum;
import com.ljf.tijian.pojo.Users;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;

public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("进入登录拦截器");

        Users users = (Users)request.getAttribute("user");
        if(users == null){
            ApiResponse apiResponse = new ApiResponse(TijianExceptionEnum.NOT_LOGIN.getCode(), TijianExceptionEnum.NOT_LOGIN.getMessage());

            response.getWriter().println(apiResponse.toJOSNString());
            return false;
        }
        System.out.println("已经登录");
        return true;
     }
}
